<?php

namespace App\V3\V3_2_7\Controllers;

use App\Helper\DBRSAUtil;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3_2_7\PageRequest;
use App\Http\Requests\V3_2_7\SmPhotoAlbumRequest;
use App\Http\Requests\V3_2_7\VerifyIdRequest;
use App\Services\CommonService;
use App\V3\V3_2_7\Models\SystemConfigModel;
use App\V3\V3_2_7\Services\SmPotoAlbumService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class SmPhotoAlbumController extends BaseController
{

    /**
     *新建
     * Author: zlf
     * Created at 2024/8/19 11:17
     * @param SmPhotoAlbumRequest $request
     * @return mixed
     */
    public function add(SmPhotoAlbumRequest $request)
    {
        $data = $request->all();
        $data['time'] = time();
        $data['main_id'] = $this->getUserId();
        $data['user_id'] = $this->getMainUserId();
        CommonService::log('添加相册', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return SmPotoAlbumService::add($data);
            });
    }

    /**
     *编辑
     * Author: zlf
     * Created at 2024/8/19 11:17
     * @param VerifyIdRequest $request
     * @return mixed
     */
    public function edit(VerifyIdRequest $request)
    {
        $data = $request->all();
        $data['time'] = time();
        $data['main_id'] = $this->getUserId();
        $data['user_id'] = $this->getMainUserId();
        CommonService::log('编辑相册', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return SmPotoAlbumService::edit($data);
            });

    }

    /**
     *删除
     * Author: zlf
     * Created at 2024/8/19 11:18
     * @param VerifyIdRequest $request
     * @return mixed
     */
    public function del(VerifyIdRequest $request)
    {
        $data = $request->all();
        $data['time'] = time();
        $data['main_id'] = $this->getUserId();
        $data['user_id'] = $this->getMainUserId();
        CommonService::log('删除相册', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return SmPotoAlbumService::del($data);
            });
    }

    /**
     *列表
     * Author: zlf
     * Created at 2024/8/19 11:18
     * @param PageRequest $request
     * @return JsonResponse
     */
    public function list(PageRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getUserId();
        $data['user_id'] = $this->getMainUserId();
        return SmPotoAlbumService::list($data);
    }

    /**
     *选择相册商品图片
     * Author: zlf
     * Created at 2024/8/19 11:15
     * @param PageRequest $request
     * @return JsonResponse
     */
    public function selAlbumGoodsImg(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getUserId();
        $data['user_id'] = $this->getMainUserId();
        return $this->success(SmPotoAlbumService::selAlbumGoodsImg($data));
    }
}
